<?php
/**
 * 
 * @author tangbl
 *
 */
class ActlotteryAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();
	}
	
	/**
	 * 抽奖活动首页
	 */
	public function index(){
		
		if(IS_AJAX){
			import('ORG.Util.Page');
			$model     = D("Actlottery");
				
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
				
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->field("description",true)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
				
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			$prz       = L('prz_page_title');
			$log       = L('prz_form_prize_log');
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'        => $row['id'],
						'title'     => $row['title'],
						'isshow'    => $row['isshow']==2?L('alt_form_ele_isshow_1'):L('alt_form_ele_isshow_0'),
						'type'      => get_array_key(C('ACTLOTTERY_TYPE'),$row['type'],true),
						'lotterypro'=> $row['lotterypro'].'/'.$row['lotterypro1'],
						'time_start'=> date('Y-m-d H:i:s',$row['time_start']),
						'time_end'  => date('Y-m-d H:i:s',$row['time_end']),
						'addtime'   => date('Y-m-d',$row['addtime']),
						'deal'=>'
				    <a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
			  		<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'" data-catetype="'.$row['catetype'].'"><i class="icon-remove"></i></a>
			  		<a href="'.__URL__.'/prize_index/actid/'.$row['id'].'" class="btn green" title="'.$prz.'"><i class=" icon-glass"></i></a>
			  		<a href="'.__URL__.'/lottery_log/actid/'.$row['id'].'" class="btn yellow" title="'.$log.'"><i class="icon-tags"></i></a>'	
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
				
		}else{
			$this->display();
		}
	}
	
	/**
	 * 添加抽奖活动
	 */
	public function add(){
		if(IS_POST){
			$allow = $_POST['allow'];
			if(is_array($allow)){
				$allow = json_encode($allow);
			}
			
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"arr"    => array(
						"time_start"  => strtotime($this->_post("time_start")),//字符串转化为时间戳
						"time_end"    => strtotime($this->_post("time_end")),
						"allow"    => $allow,
				),
			);
			parent::do_add("Actlottery",$param);
			
		}else{
			
			//语言
			$langset = $this->_param("lang","",getLang());
			$this->assign("lang",$langset);
			
			//抽奖类型
			$module = create_option_key(C("ACTLOTTERY_TYPE"),0,true);
			$this->assign("module",$module);
			
			//抽奖时间类型
			$timetype = create_option_key(C("ACTLOTTERY_TIMETTYPE"),0,true);
			$this->assign("timetype",$timetype);
			
			$sence_yhj = '';
			if(is_array(C("SENCE_ACTIVE"))){
				foreach(C("SENCE_ACTIVE") as $k=>$v){
					$sence_yhj .= '<input type="radio" name="sence" value="'.$k.'"> '.$v[0].'<br/>';
				}
			}
			
			$this->assign("sence_yhj",$sence_yhj);
			
			//用户分组
			$mgroup_select = '';
			$model_mgroup = M('mgroup');
			$data_mgroup = $model_mgroup->select();
			if($data_mgroup){
				foreach($data_mgroup as $v){
					$mgroup_select .= '<input type="checkbox" name="allow[mgroup][select][]" value="'.$v['id'].'"/> '.$v['title'].'&nbsp;&nbsp;&nbsp;';
				}				
			}
			$this->assign("mgroup_select",$mgroup_select);
			
			//用户等级
			$mlevel_select = '';
			$model_mlevel = M('mlevel');
			$data_mlevel = $model_mlevel->select();
			if($model_mlevel){
				foreach($data_mlevel as $v){
					$mlevel_select .= '<input type="checkbox" name="allow[mlevel][select][]" value="'.$v['id'].'"/> '.$v['title'].'&nbsp;&nbsp;&nbsp;';
				}				
			}
			$this->assign("mlevel_select",$mlevel_select);
			
			$this->display();
		}
	}
	
	/**
	 * 修改抽奖活动
	 */
	public function edit(){
		if(IS_POST){
			$allow = $_POST['allow'];
			if(is_array($allow)){
				$allow = json_encode($allow);
			}
			
			$param = array(
					"upload" => array("single"=>array("single")),
					"reply"  => true,
					"arr"    => array(
							"time_start"  => strtotime($this->_post("time_start")),//字符串转化为时间戳
							"time_end"    => strtotime($this->_post("time_end")),
							"allow"    => $allow,
					),
			);
			parent::do_edit("Actlottery",$param);
			
		}else{
			$model = D("Actlottery");
			
			$id   = $this->_param("id",intval,0);
			$data = $model->find($id);	
			$this->assign("detail",$data);
			
			$allow = json_decode($data['allow'],true);
			$this->assign("allow",$allow);
				
			//抽奖类型
			$module = create_option_key(C("ACTLOTTERY_TYPE"),$data['type'],true);
			$this->assign("module",$module);
				
			//抽奖时间类型
			$timetype = create_option_key(C("ACTLOTTERY_TIMETTYPE"),$data['single_type'],true);
			$this->assign("timetype",$timetype);
			
			$module    = "Actlottery";
			$single = find_thumb($data['solevar'],'single',$module);
			$this->assign("single",$single);
			
			$sence_yhj = '';
			if(is_array(C("SENCE_ACTIVE"))){
				foreach(C("SENCE_ACTIVE") as $k=>$v){
					if($data['sence']==$k){
						$sence_yhj .= '<input type="radio" name="sence" value="'.$k.'" checked/> '.$v[0].'<br/>';
					}else{
						$sence_yhj .= '<input type="radio" name="sence" value="'.$k.'"/> '.$v[0].'<br/>';
					}
				}
			}
			
			$this->assign("sence_yhj",$sence_yhj);
			
			//用户分组
			$mgroup_old = @$allow['mgroup']['select'];
			if(is_array($mgroup_old)){
				$mgroup_old = ','.implode(',',$mgroup_old);
			}
			$mgroup_select = '';
			$model_mgroup = M('mgroup');
			$data_mgroup = $model_mgroup->select();
			if($data_mgroup){
				foreach($data_mgroup as $v){
					if(stripos('|'.$mgroup_old,','.$v['id'])){
						$mgroup_select .= '<input type="checkbox" name="allow[mgroup][select][]" value="'.$v['id'].'" checked/>'.$v['title'];
					}else{
						$mgroup_select .= '<input type="checkbox" name="allow[mgroup][select][]" value="'.$v['id'].'"/>'.$v['title'];
					}
					
				}				
			}
			$this->assign("mgroup_select",$mgroup_select);
			
			//用户等级
			$mlevel_old = @$allow['mlevel']['select'];
			if(is_array($mlevel_old)){
				$mlevel_old = ','.implode(',',$mlevel_old);
			}
			$mlevel_select = '';
			$model_mlevel = M('mlevel');
			$data_mlevel = $model_mlevel->select();
			if($model_mlevel){
				foreach($data_mlevel as $v){
					if(stripos('|'.$mlevel_old,','.$v['id'])){
						$mlevel_select .= '<input type="checkbox" name="allow[mlevel][select][]" value="'.$v['id'].'" checked/>'.$v['title'].'&nbsp;&nbsp;&nbsp;';
					}else{
						$mlevel_select .= '<input type="checkbox" name="allow[mlevel][select][]" value="'.$v['id'].'"/>'.$v['title'].'&nbsp;&nbsp;&nbsp;';
					}
				}				
			}
			$this->assign("mlevel_select",$mlevel_select);
			
			$this->display();
		}
	}
	
	/**
	 * 删除抽奖活动
	 */
	public function del(){
		
		$param = array(
			"reply"  => true
		);
		parent::do_del("Actlottery",false,$param);
	}
	
	
	/**
	 * 添加奖项
	 */
	public function prize_add(){
		if(IS_POST){
			$actid = $this->_param("actid");
			$type  = $this->_param("prize_type","intval",0);
			
			$arr = array();
			if($type==1){
				$arr   = array("prize_name"=>$this->_param("prize_name"));
			}elseif($type==2){
				$jifen = $this->_param("prize_jfnum");
				$arr   = array("prize_name"=>$jifen."积分");
			}elseif($type==3){
				$arr = array("prize_name"=>"商城优惠券");
			}
			
			$param = array(
				"upload" => array("single"=>array("single")),
				"url"    => array(
					"url"=>"Actlottery/prize_index",
					"param"=>"actid=$actid",
				),
				"arr"    => $arr,
				"reply"  => true,
			);
			
			parent::do_add("ActlotteryPrize",$param);
		}else{
			//语言
			$langset = $this->_param("lang","",getLang());
			$lang    = create_option_key(C("LANG_ARRAY"),$langset);
			$this->assign("lang",$lang);
			
			$this->display();
		}
	}
	
	/**
	 * 修改奖项
	 */
	public function prize_edit(){
		
		$id    = $this->_param("id");
		$actid = $this->_param("actid");
		$type  = $this->_param("prize_type");
		
		if(IS_POST){
			
			$arr = array();
			if($type==1){
				$arr   = array("prize_name"=>$this->_param("prize_name"));
			}elseif($type==2){
				$jifen = $this->_param("prize_jfnum");
				$arr   = array("prize_name"=>$jifen."积分");
			}elseif($type==3){
				$arr = array("prize_name"=>"商城优惠券");
			}
			
			$param = array(
					"upload" => array("single"=>array("single")),
					"url"    => array(
							"url"=>"Actlottery/prize_index",
							"param"=>"actid=$actid",
					),
					"arr"    => $arr,
					"reply"  => true,
			);
				
			parent::do_edit("ActlotteryPrize",$param);
			
		}else{
			
			$model = D("ActlotteryPrize");
				
			$data  = $model->find($id);
			$this->assign("detail",$data);
			
			$module    = "ActlotteryPrize";
			$single = find_thumb($data['solevar'],'single',$module);
			$this->assign("single",$single);
			
			//语言
			$lang    = create_option_key(C("LANG_ARRAY"),$data['lang']);
			$this->assign("lang",$lang);
			
			$this->display();
		}
	}
	
	/**
	 * 奖项列表
	 */
	public function prize_index(){

		$actid  = $this->_param('actid');
		
		if(IS_AJAX){
			import('ORG.Util.Page');
			$model  = M("ActlotteryPrize");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 and actid=".$actid.$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						->field("description",true)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){				
				if($row['prize_type']==1){
					$prize_type = '实物';
					$prize_name = $row['prize_name'];
				}elseif($row['prize_type']==2){
					$prize_type = '积分';
					$prize_name = $row['prize_jfnum'].'积分';
				}elseif($row['prize_type']==3){
					$prize_type = '优惠劵';
					$model_yhj = M('Actyhj');
					$data_yhj = $model_yhj->find($row['prize_yhj_id']);
					$prize_name = $data_yhj['title'];
				}				
				
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'          => $row['id'],
						'awards_name' => $row['awards_name'],
						'prize_type'  => $prize_type,
						'prize_name'  => $prize_name,
						'prize_num'   => $row['prize_num'],
						'surplus_num' => $row['surplus_num'],
						'probability' => $row['prize_probability'],
						'addtime'     => date('Y-m-d',$row['addtime']),
						'deal'=>' 
			  <a href="'.__URL__.'/prize_edit/id/'.$row['id'].'/actid/'.$actid.'" class="btn blue icn-only" title="'.L('cfg_list_itf_btn_modify').'"><i class="icon-edit"></i></a> 
			  <a href="#" class="btn red icn-only delAlone" title="'.L('cfg_list_itf_btn_delete').'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> '
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			
			$this->assign("actid",$actid);
			$this->display();
		}
	}
	

	/**
	 * 删除奖项
	 */
	public function prize_del(){
		$param = array(
			"reply"  => true
		);
		parent::do_del("ActlotteryPrize",true,$param);
	}
	
	/**
	 * 抽奖纪录
	 */
	public function lottery_log(){
		
		$actid  = $this->_param('actid');
		
		if(IS_AJAX){
			import('ORG.Util.Page');// 导入分页类
			$model  = M("ActlotteryLog");
			$module = D("Actlottery");
				
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
				
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 and is_winning=1 and actid=".$actid.$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						->field("description",true)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				
				if($row['prize_id']){
					$detail = $module->getPrizeDetail($row['prize_id']);
					$prize  = $detail['prize_name'];
				}else{
					$prize  = "";
				}
				
				if($row['actid']){
					$lottery = $module->getLotteryDetail($row['actid']);
					$title   = $lottery['title'];
				}
				
				if($row['is_convert']!=1){
					$deal = '<a href="'.__URL__.'/editLogStatus/id/'.$row['id'].'/actid/'.$row['actid'].'" class="btn green icn-only edit" title="'.L('alt_form_ele_convert').'" data-id="'.$row['id'].'"><i class="icon-ok-sign"></i></a>';
				}else{
					$deal = "";
				}
				
				$entry = array('id'=>$row['id'],
						'cell'=>array(
								'id'           => $row['id'],
								'realname'     => $row['realname'],
								'sex'          => $row['sex']==1?L('cfg_form_ele_sex1'):L('cfg_form_ele_sex2'),
								'mobile'       => $row['mobile'],
								'address'      => $row['address'],
								'lottery_time' => date('Y-m-d',$row['lottery_time']),
								'is_winning'   => $row['is_winning']==1?"是":"否",
								'is_convert'   => $row['is_convert']==1?"是":"否",
								'prize_id'     => $prize,
								'actid'        => $title,
								'addtime'      => date('Y-m-d',$row['addtime']),
								//'deal'=>' <a href="'.__URL__.'/prize_edit/id/'.$row['id'].'/actid/'.$actid.'" class="btn blue icn-only" title="'.L('cfg_list_itf_btn_modify').'"><i class="icon-edit"></i></a>
					  //<a href="#" class="btn red icn-only delAlone" title="'.L('cfg_list_itf_btn_delete').'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>
					  //',
								'deal'=>'<a href="#" class="btn red icn-only delAlone" title="'.L('cfg_list_itf_btn_delete').'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>
					  '.$deal
						),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
				
			$this->assign("actid",$actid);
			$this->display();
		}
	}
	
	/**
	 * ajax验证奖品中奖概率
	 * 
	 */
	public function ajaxPrizeProbability(){
		
		$param = $this->_param("param",'intval',0);
		$actid = $this->_param("actid",'intval',0);
		$num   = $this->_param("num",'intval',0);
		$id    = $this->_param("id",'intval',0);
		
		
		$model = D("Actlottery");
		$data  = $model->prizeProbability($actid,$id);
		$this->ajaxReturn($data);
	}
	
	/**
	 * 删除领奖记录
	 */
	public function log_del(){
		
		$param = array(
			"reply"  => true
		);
		parent::do_del("ActlotteryLog",true,$param);
	}
	
	/**
	 * 更新兑奖状态
	 */
	public function editLogStatus(){
		
		$id    = $this->_param("id","intval",0);
		$actid = $this->_param("actid","intval",0);
		
		$model = M("ActlotteryLog");
		
		$map['id']         = $id;
		$map['is_convert'] = 1;
		$data = $model->save($map);
		
		$this->success(L('alt_form_ele_convert_tip'),U('Actlottery/lottery_log','actid='.$actid));
		
	}
}
?>